import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.event.ActionEvent;
import java.text.DecimalFormat;
import java.util.*;
import java.io.*;

public class MyActionListener implements java.awt.event.ActionListener {
	private MyTableModel tableModel;
	private MyJTable table;
	
	public MyActionListener(MyJTable t) {
		table = t; 
		tableModel = (MyTableModel)table.getModel();
	}
	
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("add")) {
			
			JTextField name = new JTextField();
			
			Vector v = new Vector();
			for (int i=1; i<=31; v.add(i++));
			JComboBox day = new JComboBox(v);
			
			v = new Vector();
			for (int i=1; i<=12; v.add(i++));
			JComboBox month = new JComboBox(v);
			
			v = new Vector();
			Calendar cal = Calendar.getInstance();
			cal.setTime(new Date()); 
			int curYear = cal.get(Calendar.YEAR);
			for (int i = curYear; i>= 1901; v.add(i--));
			JComboBox year = new JComboBox(v);
			
			Object[] add = {"name", name, "day", day, "month", month, "year", year};
			JOptionPane pane = new JOptionPane(add, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
			pane.createDialog(null, "add birthday").setVisible(true);

			DecimalFormat df =   new DecimalFormat  ( "00" );
			
			Vector row = new Vector();
			row.add(name.getText());
			row.add(year.getSelectedItem() +"-"+ df.format(month.getSelectedItem()) +"-"+ df.format(day.getSelectedItem()));
			tableModel.addRow(row);
			
			tableModel.sort();
			
		} else if (e.getActionCommand().equals("edit")) {
		
			int selectedRow = table.getSelectedRow();
			String date = (String)(table.getValueAt(selectedRow,1));

			JTextField name = new JTextField((String)(table.getValueAt(selectedRow,0)));
			
			Vector v = new Vector();
			for (int i=1; i<=31; v.add(i++));
			JComboBox day = new JComboBox(v);
			day.setSelectedItem(Integer.parseInt(date.substring(8)));
			
			v = new Vector();
			for (int i=1; i<=12; v.add(i++));
			JComboBox month = new JComboBox(v);
			month.setSelectedItem(Integer.parseInt(date.substring(5,7)));
			
			v = new Vector();
			Calendar cal = Calendar.getInstance();
			cal.setTime(new Date()); 
			int curYear = cal.get(Calendar.YEAR);
			for (int i = curYear; i>= 1901; v.add(i--));
			JComboBox year = new JComboBox(v);
			year.setSelectedItem(Integer.parseInt(date.substring(0,4)));
			
			Object[] edit = {"name", name, "day", day, "month", month, "year", year};
			JOptionPane pane = new JOptionPane(edit, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
			pane.createDialog(null, "edit birthday").setVisible(true);

			DecimalFormat df =   new DecimalFormat  ( "00" );
			
			Vector row = new Vector();
			row.add(name.getText());
			row.add(year.getSelectedItem() +"-"+ df.format(month.getSelectedItem()) +"-"+ df.format(day.getSelectedItem()));
			
			tableModel.removeRow(selectedRow);	
			tableModel.addRow(row);
			tableModel.sort();
			
		} else if (e.getActionCommand().equals("delete")) {
			tableModel.removeRow(table.getSelectedRow());
		}
				
		String jar = getClass().getProtectionDomain().getCodeSource().getLocation().getFile().replaceAll("%20", " ");
		File jarFile = new File(jar);
		File file = new File(jarFile.getParent()+"/birthdays");

		if (!file.isFile()) 
			JOptionPane.showMessageDialog(null, "Couldn't open birthday file.", "Error", JOptionPane.WARNING_MESSAGE);
		else try {
			PrintWriter out = new PrintWriter(file);
			for (int i = 0; i<tableModel.getRowCount(); i++)
				out.println("\""+tableModel.getValueAt(i,0)+"\",#"+tableModel.getValueAt(i,1)+"#");
			out.close();
		} catch (Exception exp) {
			JOptionPane.showMessageDialog(null, "Writing birthday file failed.", "Error", JOptionPane.WARNING_MESSAGE);
		}
	}
}
